package com.example.xlmkotlinwanandroid.ui.adapter

import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import android.widget.ImageView
import androidx.core.content.ContextCompat.startActivity
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.example.xlmkotlinwanandroid.model.bean.BannerBean
import com.example.xlmkotlinwanandroid.ui.activity.WebActivity
import com.youth.banner.adapter.BannerAdapter


class MyBannerAdapter(var mContext: Context, mDatas: List<BannerBean>) :
    BannerAdapter<BannerBean, MyBannerAdapter.BannerViewHolder?>(mDatas) {


    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val imageView = ImageView(parent.context)
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        imageView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP

        return BannerViewHolder(imageView)
    }

    override fun onBindView(
        holder: BannerViewHolder?,
        data: BannerBean?,
        position: Int,
        size: Int
    ) {
        holder?.let {
            Glide.with(holder.itemView)
                .load(data?.imagePath)
                .apply(RequestOptions.bitmapTransform(RoundedCorners(30)))
                .into(holder.imageView)
        }
        //绑定点击事件
        holder?.imageView?.setOnClickListener {
            var intent = Intent(mContext, WebActivity::class.java)
            intent.putExtra("url",data?.url)
            intent.putExtra("title",data?.title)
            mContext.startActivity(intent)
        }

    }

    inner class BannerViewHolder(view: ImageView) : RecyclerView.ViewHolder(view) {
        var imageView: ImageView = view
    }

    fun setData(bannerData: List<BannerBean>) {
        mDatas.addAll(bannerData)
    }

}